package com.project.dao.impl;

import com.project.bean.ClassBean;
import com.project.bean.StudentBean;

import java.sql.*;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class BaseDao {
    protected Connection con;//连接对象

    protected PreparedStatement ps;//sql语句执行对象

    protected ResultSet rs;   //结果集

    /**
     * 建立连接
     */
    public void setConnection(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            this.con= DriverManager.getConnection("jdbc:mysql://localhost:6789/Test?characterEncoding=utf-8",
                    "root","lovo");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭连接
     */
    public void closeConnection(){
        try {
            if (rs!=null){
                rs.close();
            }
            if (ps!=null){
                ps.close();
            }
            if (con!=null){
                con.close();
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public List getObject(ResultSet rs) {

        ArrayList<Object> list = new ArrayList<>();
        this.rs = rs;
        try {
            while (this.rs.next()){
                StudentBean studentBean=new StudentBean();
                studentBean.setId(rs.getInt("pk_studentId"));
                studentBean.setName(rs.getString("s_name"));
                studentBean.setClassName(new ClassBean(rs.getString("c_name")));
                list.add(studentBean);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
